package com.bdmc.web.controller.car;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bdmc.car.domain.bo.CarMaintenanceDetailBo;
import com.bdmc.car.domain.vo.CarMaintenanceDetailVo;
import com.bdmc.car.service.ICarMaintenanceDetailService;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 * 维修详单控制器
 *
 * @author bdmc
 * @date 2023-01-06
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/car/maintenanceDetail")
public class CarMaintenanceDetailController extends BaseController {

    private final ICarMaintenanceDetailService iCarMaintenanceDetailService;

    /**
     * 查询维修详单列表
     */
    @SaCheckPermission("car:maintenanceDetail:list")
    @GetMapping("/list")
    public TableDataInfo<CarMaintenanceDetailVo> list(@Validated(QueryGroup.class) CarMaintenanceDetailBo bo, PageQuery pageQuery) {
        return iCarMaintenanceDetailService.mySelectPage(bo, pageQuery);
    }

    /**
     * 导出维修详单列表
     */
    @SaCheckPermission("car:maintenanceDetail:export")
    @Log(title = "维修详单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated CarMaintenanceDetailBo bo, HttpServletResponse response) {
        List<CarMaintenanceDetailVo> list = iCarMaintenanceDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "维修详单", CarMaintenanceDetailVo.class, response);
    }

    /**
     * 获取维修详单详细信息
     */
    @SaCheckPermission("car:maintenanceDetail:query")
    @GetMapping("/{detailId}")
    public AjaxResult<CarMaintenanceDetailVo> getInfo(@PathVariable("detailId") Integer detailId) {
        return AjaxResult.success(iCarMaintenanceDetailService.queryById(detailId));
    }

    /**
     * 新增维修详单
     */
    @SaCheckPermission("car:maintenanceDetail:add")
    @Log(title = "维修详单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody CarMaintenanceDetailBo bo) {
        return toAjax(iCarMaintenanceDetailService.insertByBo(bo));
    }

    /**
     * 修改维修详单
     */
    @SaCheckPermission("car:maintenanceDetail:edit")
    @Log(title = "维修详单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody CarMaintenanceDetailBo bo) {
        return toAjax(iCarMaintenanceDetailService.updateByBo(bo));
    }

    /**
     * 删除维修详单
     */
    @SaCheckPermission("car:maintenanceDetail:remove")
    @Log(title = "维修详单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{detailIds}")
    public AjaxResult<Void> remove(@PathVariable Integer[] detailIds) {
        return toAjax(iCarMaintenanceDetailService.deleteWithValidByIds(Arrays.asList(detailIds), true));
    }
}
